package com.igroupes.ovineherd.model.Style;

import cn.hutool.core.lang.Singleton;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.SelectKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class SelectStyle extends BasicStyle {
    protected String mode = SelectKey.MUTIL.getCode(); //模式 single单列 mutil多列

    private SelectStyle(String label, Object value,List<Group> options) {
        super(label, value);
        this.type = FormKey.SELECT.getCode();
        this.options = options;
    }
    private SelectStyle(String label, Object value,List<Group> options, String desc) {
        this(label, value,options);
        this.desc = desc;
    }
    public SelectStyle(String label, Object value, List<Group> options, String desc, String mode) {
        this(label, value,options);
        this.desc = desc;
        this.mode = mode;
    }
    private List<Group> options;

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof SelectStyle)){
            throw new ModelException("校验style类类型错误");
        }
        SelectStyle style = (SelectStyle) o;
        if(!style.getDisabled()){
            super.verify(o);
            Group group = Singleton.get(Group.class);
            Integer check = 1;
            for (Group g :
                    style.getOptions()) {
                if(!(g instanceof Group)){
                    throw new ModelException(style.getLabel()+"传值类型错误");
                }
                if(!g.getDisabled() && g.getChecked()){
                    check++;
                }
                group.verify(g);
            }
            if(check != 1){
                throw new ModelException(style.getLabel()+"选中值不能为空且只能选中一个");
            }
        }
        return true;
    }
}
